package com.yulebaby.callsenter.record;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;
import android.telephony.TelephonyManager;

import com.orhanobut.logger.Logger;
import com.yulebaby.callsenter.R;
import com.yulebaby.callsenter.global.App;
import com.yulebaby.callsenter.record.manager.AudioFileFunc;
import com.yulebaby.callsenter.record.manager.RecordManager;
import com.yulebaby.callsenter.task.ReadContactTask;
import com.yulebaby.callsenter.ui.activity.MainActivity;
import com.yulebaby.callsenter.utils.DateUtil;


public class PhoneService extends Service {


    private OutCallReceiver mReceiver;


    private String mOutingNumber;
    private String mCurrentTime;
    private String imCommingNumber = null;
    private NotificationManager mNotificationManager;

    /**
     * Notification的ID
     */
    private static final int notifyId = 10002;

    class OutCallReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
                mOutingNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
                App.mCallFlag = true;
            } else {
                switch (mTelephonyManager.getCallState()) {
                    case TelephonyManager.CALL_STATE_RINGING:
                        if (imCommingNumber == null) {
                            imCommingNumber = intent.getStringExtra("incoming_number");
                        }
                        break;
                    case TelephonyManager.CALL_STATE_OFFHOOK:
                        mCurrentTime = DateUtil.getCurrentTime();
                        if (imCommingNumber == null) {
                            imCommingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
                        }
                        if (App.mCallFlag) {
                            AudioFileFunc.AUDIO_AMR_FILENAME = mOutingNumber + "_Out_" + mCurrentTime + ".amr";
                            RecordManager.getInstance().record(RecordManager.FLAG_AMR);
//                            AudioFileFunc.AUDIO_WAV_FILENAME = mOutingNumber + "_Out_" + mCurrentTime + ".wav";
//                            RecordManager.getInstance().record(RecordManager.FLAG_WAV);
                        } else if (App.mCallFlag == false && imCommingNumber != null) {
                            AudioFileFunc.AUDIO_AMR_FILENAME = imCommingNumber + "_In_" + mCurrentTime + ".amr";
                            RecordManager.getInstance().record(RecordManager.FLAG_AMR);
//                            AudioFileFunc.AUDIO_WAV_FILENAME = imCommingNumber + "_In_" + mCurrentTime + ".wav";
//                            RecordManager.getInstance().record(RecordManager.FLAG_WAV);
                        }
                        break;
                    case TelephonyManager.CALL_STATE_IDLE:
                        RecordManager.getInstance().stop();
                        ReadContactTask.getCallLog();
                        App.isFinish = true;
                        imCommingNumber = null;
                        break;
                    default:
                        break;
                }
            }
        }
    }


    private TelephonyManager mTelephonyManager;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mTelephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        IntentFilter filter = new IntentFilter();
        filter.setPriority(Integer.MAX_VALUE);
        filter.addAction("android.intent.action.NEW_OUTGOING_CALL");
        filter.addAction("android.intent.action.PHONE_STATE");
        mReceiver = new OutCallReceiver();
        registerReceiver(mReceiver, filter);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        showCzNotify();
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        startService(new Intent(this, PhoneService.class));
        unregisterReceiver(mReceiver);
    }

    /**
     * 显示常驻通知栏
     */
    public void showCzNotify() {
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
        Intent resultIntent = new Intent(this, MainActivity.class);
        resultIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        mBuilder.setSmallIcon(R.mipmap.ic_launcher)
                .setTicker("云呼叫中心")
                .setContentTitle("云呼叫中心")
                .setContentText("呼叫中心服务已启动")
                .setContentIntent(pendingIntent);
        Notification mNotification = mBuilder.build();
        mNotification.flags = Notification.FLAG_ONGOING_EVENT;
        mNotificationManager.notify(notifyId, mNotification);
    }

}
